#
#        Python GUI - Components - Swing
#

#TODO: make Component.destroy() work better!

from GComponents import Component as GComponent
from Events import Event
from Colors import _from_j_color
from Fonts import _from_j_font
from Geometry import sub_pt

import javax.swing as swing
from java.awt.event import ComponentListener,KeyListener,MouseListener

from JHacks import *

_j_shared_border=None #Shared singleton object used for rendering borders around objects

class Component(GComponent,ComponentListener,KeyListener,MouseListener):
    #The component is its own event listener, so it's subclassed from the listener classes
    
    def __init__(self,**kwds):
        GComponent.__init__(self,**kwds)

        #Register for events
        
        self._j_obj.addComponentListener(self)
        
        if hasattr(self,"key_down") or hasattr(self,"key_up"):
            self._j_obj.addKeyListener(self)
            
        if hasattr(self,"mouse_down"):
            self._j_obj.addMouseListener(self)
        
        #Default size
        
        psize=self._j_obj.getPreferredSize()
        if self.width==0:
            self.width=psize.width
        if self.height==0:
            self.height=psize.height
        
        self._old_size=self.size
    
    def get_enabled(self):
        return bool(self._j_obj.isEnabled())
    
    def set_enabled(self, v):
        self._j_obj.setEnabled(int(v))
    
    def get_color(self):
        return _from_j_color(self._j_obj.getForeground())
    
    def set_color(self, v):
        self._j_obj.setForeground(v._j_color)
            
    def get_font(self):
        return _from_j_font(self._j_obj.getFont())
    
    def set_font(self, v):
        self._j_obj.setFont(v._j_font)
    
    def get_bounds(self):
        pos=self._j_obj.getLocation()
        size=self._j_obj.getSize()
        return (pos.x,pos.y,pos.x+size.width,pos.y+size.height)
    
    def set_bounds(self, (l, t, r, b)):
        self._j_obj.setBounds(l,t,(r-l),(b-t))
    
    def get_visible(self):
        return self._j_obj.isVisible()
    
    def set_visible(self,visible):
        self._j_obj.setVisible(int(visible))
    
    def get_border(self):
        return not not self._j_obj.getBorder()
    
    def set_border(self,border):
        if border:
            if not _j_shared_border: _j_shared_border=swing.BorderFactory.createEtchedBorder()
            self._j_obj.setBorder(_j_shared_border)
        else:
            self._j_obj.setBorder(None)
        
    def become_target(self):
        self._j_obj.grabFocus()
    
    def is_target(self):
        return self._j_obj.hasFocus()
    
    def destroy(self):
        GComponent.destroy(self)
        self._j_obj=None #Remove all known references and hope the component gets freed
    
    #Event callbacks
    
    def componentHidden(self,e):
        pass
    
    def componentMoved(self,e):
        pass
    
    def componentResized(self,e):
        if not hasattr(self,"_old_size"): return #Don't want resize events unless __init__ is finished
        
        delta=sub_pt(self._old_size,self.size)
        self.resized(delta)
        
        self._old_size=self.size
    
    def componentShown(self,e):
        pass
    
    def keyPressed(self,e):
        self.key_down(Event(e))
    
    def keyReleased(self,e):
        self.key_up(Event(e))
    
    def keyTyped(self,e):
        pass
    
    def mousePressed(self,e):
        self.mouse_down(Event(e))
    
    def mouseEntered(self,e):
        pass
    
    def mouseReleased(self,e):
        pass
    
    def mouseExited(self,e):
        pass
    
    def mouseClicked(self,e):
        pass
